Make RelativeDateFormatter available to plugins Change-Id: I9ab4a50c5eceec3a841c79f120ee67f428038cce Signed-off-by: Edwin Kempin <edwin.kempin@sap.com> 
diff --git a/gerrit-gwtui-common/BUCK b/gerrit-gwtui-common/BUCK index 6ee0336..3977487 100644 --- a/gerrit-gwtui-common/BUCK +++ b/gerrit-gwtui-common/BUCK 
@@ -63,3 +63,16 @@  name = 'diffy_image_files',  resources = DIFFY,  ) + +java_test( + name = 'client_tests', + srcs = glob(['src/test/java/**/*.java']), + deps = [ + ':client', + '//lib:junit', + '//lib/jgit:jgit', + ], + source_under_test = [':client'], + vm_args = ['-Xmx512m'], + visibility = ['//tools/eclipse:classpath'], +) 
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonConstants.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonConstants.java new file mode 100644 index 0000000..032c47c --- /dev/null +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonConstants.java 
@@ -0,0 +1,36 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.client; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.i18n.client.Constants; + +public interface CommonConstants extends Constants { + public static final CommonConstants C = GWT.create(CommonConstants.class); + + String inTheFuture(); + String month(); + String months(); + String year(); + String years(); + + String oneSecondAgo(); + String oneMinuteAgo(); + String oneHourAgo(); + String oneDayAgo(); + String oneWeekAgo(); + String oneMonthAgo(); + String oneYearAgo(); +} 
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonConstants.properties b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonConstants.properties new file mode 100644 index 0000000..3202bfc --- /dev/null +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonConstants.properties 
@@ -0,0 +1,13 @@ +inTheFuture = in the future +month = month +months = months +years = years +year = year + +oneSecondAgo = 1 second ago +oneMinuteAgo = 1 minute ago +oneHourAgo = 1 hour ago +oneDayAgo = 1 day ago +oneWeekAgo = 1 week ago +oneMonthAgo = 1 month ago +oneYearAgo = 1 year ago 
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonMessages.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonMessages.java new file mode 100644 index 0000000..aa5e3cf --- /dev/null +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonMessages.java 
@@ -0,0 +1,33 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.client; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.i18n.client.Messages; + +public interface CommonMessages extends Messages { + public static final CommonMessages M = GWT.create(CommonMessages.class); + + String secondsAgo(long seconds); + String minutesAgo(long minutes); + String hoursAgo(long hours); + String daysAgo(long days); + String weeksAgo(long weeks); + String monthsAgo(long months); + String yearsAgo(long years); + String years0MonthsAgo(long years, String yearLabel); + String yearsMonthsAgo(long years, String yearLabel, long months, + String monthLabel); +} 
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonMessages.properties b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonMessages.properties new file mode 100644 index 0000000..738602e --- /dev/null +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/CommonMessages.properties 
@@ -0,0 +1,9 @@ +secondsAgo = {0} seconds ago +minutesAgo = {0} minutes ago +hoursAgo = {0} hours ago +daysAgo = {0} days ago +weeksAgo = {0} weeks ago +monthsAgo = {0} months ago +years0MonthsAgo = {0} {1} ago +yearsMonthsAgo = {0} {1}, {2} {3} ago +yearsAgo = {0} years ago 
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java new file mode 100644 index 0000000..eb4b0ba --- /dev/null +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java 
@@ -0,0 +1,138 @@ +// Copyright (C) 2013 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.client; + +import static com.google.gerrit.client.CommonConstants.C; +import static com.google.gerrit.client.CommonMessages.M; + +import java.util.Date; + +/** + * Formatter to format timestamps relative to the current time using time units + * in the format defined by {@code git log --relative-date}. + */ +public class RelativeDateFormatter { + static final long SECOND_IN_MILLIS = 1000; + static final long MINUTE_IN_MILLIS = 60 * SECOND_IN_MILLIS; + static final long HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS; + static final long DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS; + static final long WEEK_IN_MILLIS = 7 * DAY_IN_MILLIS; + static final long MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS; + static final long YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS; + + /** + * @param when {@link Date} to format + * @return age of given {@link Date} compared to now formatted in the same + * relative format as returned by {@code git log --relative-date} + */ + @SuppressWarnings("boxing") + public static String format(Date when) { + long ageMillis = (new Date()).getTime() - when.getTime(); + + // shouldn't happen in a perfect world + if (ageMillis < 0) { + return C.inTheFuture(); + } + + // seconds + if (ageMillis < upperLimit(MINUTE_IN_MILLIS)) { + long seconds = round(ageMillis, SECOND_IN_MILLIS); + if (seconds == 1) { + return C.oneSecondAgo(); + } else { + return M.secondsAgo(seconds); + } + } + + // minutes + if (ageMillis < upperLimit(HOUR_IN_MILLIS)) { + long minutes = round(ageMillis, MINUTE_IN_MILLIS); + if (minutes == 1) { + return C.oneMinuteAgo(); + } else { + return M.minutesAgo(minutes); + } + } + + // hours + if (ageMillis < upperLimit(DAY_IN_MILLIS)) { + long hours = round(ageMillis, HOUR_IN_MILLIS); + if (hours == 1) { + return C.oneHourAgo(); + } else { + return M.hoursAgo(hours); + } + } + + // up to 14 days use days + if (ageMillis < 14 * DAY_IN_MILLIS) { + long days = round(ageMillis, DAY_IN_MILLIS); + if (days == 1) { + return C.oneDayAgo(); + } else { + return M.daysAgo(days); + } + } + + // up to 10 weeks use weeks + if (ageMillis < 10 * WEEK_IN_MILLIS) { + long weeks = round(ageMillis, WEEK_IN_MILLIS); + if (weeks == 1) { + return C.oneWeekAgo(); + } else { + return M.weeksAgo(weeks); + } + } + + // months + if (ageMillis < YEAR_IN_MILLIS) { + long months = round(ageMillis, MONTH_IN_MILLIS); + if (months == 1) { + return C.oneMonthAgo(); + } else { + return M.monthsAgo(months); + } + } + + // up to 5 years use "year, months" rounded to months + if (ageMillis < 5 * YEAR_IN_MILLIS) { + long years = ageMillis / YEAR_IN_MILLIS; + String yearLabel = (years > 1) ? C.years() : C.year(); + long months = round(ageMillis % YEAR_IN_MILLIS, MONTH_IN_MILLIS); + String monthLabel = (months > 1) ? C.months() : (months == 1 ? C.month() : ""); + if (months == 0) { + return M.years0MonthsAgo(years, yearLabel); + } else { + return M.yearsMonthsAgo(years, yearLabel, months, monthLabel); + } + } + + // years + long years = round(ageMillis, YEAR_IN_MILLIS); + if (years == 1) { + return C.oneYearAgo(); + } else { + return M.yearsAgo(years); + } + } + + private static long upperLimit(long unit) { + return unit + unit / 2; + } + + private static long round(long n, long unit) { + return (n + unit / 2) / unit; + } +} 
diff --git a/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java b/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java new file mode 100644 index 0000000..7c71921 --- /dev/null +++ b/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java 
@@ -0,0 +1,99 @@ +// Copyright (C) 2013 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.client; + +import static com.google.gerrit.client.RelativeDateFormatter.DAY_IN_MILLIS; +import static com.google.gerrit.client.RelativeDateFormatter.HOUR_IN_MILLIS; +import static com.google.gerrit.client.RelativeDateFormatter.MINUTE_IN_MILLIS; +import static com.google.gerrit.client.RelativeDateFormatter.SECOND_IN_MILLIS; +import static com.google.gerrit.client.RelativeDateFormatter.YEAR_IN_MILLIS; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.jgit.util.RelativeDateFormatter; +import org.junit.Test; + +import java.util.Date; + +public class RelativeDateFormatterTest { + + private static void assertFormat(long ageFromNow, long timeUnit, + String expectedFormat) { + Date d = new Date(System.currentTimeMillis() - ageFromNow * timeUnit); + String s = RelativeDateFormatter.format(d); + assertEquals(expectedFormat, s); + } + + @Test + public void testFuture() { + assertFormat(-100, YEAR_IN_MILLIS, "in the future"); + assertFormat(-1, SECOND_IN_MILLIS, "in the future"); + } + + @Test + public void testFormatSeconds() { + assertFormat(1, SECOND_IN_MILLIS, "1 seconds ago"); + assertFormat(89, SECOND_IN_MILLIS, "89 seconds ago"); + } + + @Test + public void testFormatMinutes() { + assertFormat(90, SECOND_IN_MILLIS, "2 minutes ago"); + assertFormat(3, MINUTE_IN_MILLIS, "3 minutes ago"); + assertFormat(60, MINUTE_IN_MILLIS, "60 minutes ago"); + assertFormat(89, MINUTE_IN_MILLIS, "89 minutes ago"); + } + + @Test + public void testFormatHours() { + assertFormat(90, MINUTE_IN_MILLIS, "2 hours ago"); + assertFormat(149, MINUTE_IN_MILLIS, "2 hours ago"); + assertFormat(35, HOUR_IN_MILLIS, "35 hours ago"); + } + + @Test + public void testFormatDays() { + assertFormat(36, HOUR_IN_MILLIS, "2 days ago"); + assertFormat(13, DAY_IN_MILLIS, "13 days ago"); + } + + @Test + public void testFormatWeeks() { + assertFormat(14, DAY_IN_MILLIS, "2 weeks ago"); + assertFormat(69, DAY_IN_MILLIS, "10 weeks ago"); + } + + @Test + public void testFormatMonths() { + assertFormat(70, DAY_IN_MILLIS, "2 months ago"); + assertFormat(75, DAY_IN_MILLIS, "3 months ago"); + assertFormat(364, DAY_IN_MILLIS, "12 months ago"); + } + + @Test + public void testFormatYearsMonths() { + assertFormat(366, DAY_IN_MILLIS, "1 year ago"); + assertFormat(380, DAY_IN_MILLIS, "1 year, 1 month ago"); + assertFormat(410, DAY_IN_MILLIS, "1 year, 2 months ago"); + assertFormat(2, YEAR_IN_MILLIS, "2 years ago"); + assertFormat(1824, DAY_IN_MILLIS, "4 years, 12 months ago"); + } + + @Test + public void testFormatYears() { + assertFormat(5, YEAR_IN_MILLIS, "5 years ago"); + assertFormat(60, YEAR_IN_MILLIS, "60 years ago"); + } +}